package vashion.azeroth.core.manager.item;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import vashion.azeroth.contant.GenericErrorEnum;
import vashion.azeroth.core.dao.BaseDao;
import vashion.azeroth.core.item.dao.ItemLenuxBrandDao;
import vashion.azeroth.core.item.pojo.ItemLenuxBrandDO;
import vashion.azeroth.core.manager.impl.BaseManagerImpl;
import vashion.azeroth.core.result.ApiResult;

/**
 * Created by Loki on 17/5/24. lenux品牌管理
 */
@Service
public class ItemLenuxBrandManager extends BaseManagerImpl<ItemLenuxBrandDO> {

	private final static Logger log = LoggerFactory.getLogger(ItemLenuxBrandManager.class);

	@Autowired
	private ItemLenuxBrandDao itemLenuxBrandDao;

	@Override
	public BaseDao<ItemLenuxBrandDO> getDao() {
		return itemLenuxBrandDao;
	}

	/**
	 * 按照lenux品牌名精确查找品牌类
	 * 
	 * @param lenuxBrandTitle
	 * @return lenux品牌名为lenuxBrandtitle的品牌类
	 */
	public ApiResult<ItemLenuxBrandDO> findByLenuxBrandTitle(String lenuxBrandTitle) {
		try {
			ItemLenuxBrandDO itemLenuxBrandDO = itemLenuxBrandDao.findByLenuxBrandTitle(lenuxBrandTitle);
			return ApiResult.newSuccessResult(itemLenuxBrandDO);
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

	/**
	 * 保存lenux品牌和本系统的品牌的关联
	 * 
	 * @param itemLenuxBrandDO
	 * @return lenux品牌和系统品牌关联保存结果
	 */
	public ApiResult<Boolean> publishLenuxBrand(ItemLenuxBrandDO itemLenuxBrandDO) {
		ApiResult<ItemLenuxBrandDO> yvoguoBrandResult = this
				.findByLenuxBrandTitle(itemLenuxBrandDO.getLenuxBrandTitle());
		try {
			if (yvoguoBrandResult.isSuccess()) {
				if (null == yvoguoBrandResult.getData()) {
					itemLenuxBrandDao.save(itemLenuxBrandDO);
					return ApiResult.newSuccessResult(true);
				} else {
					return ApiResult.newSuccessResult(false);
				}
			} else {
				return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
						GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
			}
		} catch (Exception e) {
			return ApiResult.newErrorResult(GenericErrorEnum.SYSTEM_ERROR.getErrorCode(),
					GenericErrorEnum.SYSTEM_ERROR.getErrorMessage());
		}
	}

}
